package com.ylw.basics.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.ylw.basics.zuul.build.GatewayDirector;
import com.ylw.basics.zuul.chain.ResponsibilityClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * description: 网关拦截
 * create by: YangLinWei
 * create time: 2020/5/20 9:10 上午
 */
@Component
@Slf4j
public class GatewayFilter extends ZuulFilter {

    @Autowired
    private GatewayDirector gatewayDirector;

    @Autowired
    private ResponsibilityClient responsibilityClient;

    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        HttpServletResponse response = ctx.getResponse();
        response.setContentType("UTF-8");
        // 1.获取ip地址
        String ipAddres = getIpAddr(request);
        if (StringUtils.isEmpty(ipAddres)) {
            resultError(ctx, "未能够获取到ip地址");
        }
        //建造者模式
        /*gatewayDirector.direcot(ctx, ipAddres, response, request);*/

        //责任链模式
        responsibilityClient.responsibility(ctx, ipAddres, request, response);
        return null;
    }

    private void resultError(RequestContext ctx, String errorMsg) {
        ctx.setResponseStatusCode(401);
        ctx.setSendZuulResponse(false);
        ctx.setResponseBody(errorMsg);

    }

    @Override
    public boolean shouldFilter() {

        return true;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 在方法之前拦截
     *
     * @return
     */
    @Override
    public String filterType() {

        return "pre";
    }

    /**
     * 获取Ip地址
     *
     * @param request
     * @return
     */
    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
